package com.ruoyi.web.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysCertificateTeacher;
import com.ruoyi.system.domain.SysOrganization;
import com.ruoyi.system.service.ISysOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @Author 柳
 * @Date 2021/2/25
 * @Description TODO
 */
@RestController
@RequestMapping("/system/organization")
public class SysOrganizationController {

    @Autowired
    private ISysOrganizationService sysOrganizationService;


    @ApiOperation("修改机构印章文件路径")
    @PutMapping("/updateSysOrganizationSeal")
    public AjaxResult updateSysOrganizationSeal(@RequestParam("sealfile") MultipartFile sealfile,
                                          @RequestParam("organizationid") Integer organizationid) {
        SysOrganization sysOrganization=new SysOrganization();
        sysOrganization.setSealfile(sealfile);
        sysOrganization.setOrganizationid(organizationid);
        sysOrganizationService.updateSysOrganizationSeal(sysOrganization);
        return AjaxResult.success();
    }


    @ApiOperation("/修改机构状态为已启用")
    @PutMapping("/updateSysOrganizationState/{organizationid}")
    public AjaxResult updateSysOrganizationState(@PathVariable Integer organizationid) {
        if(organizationid==0 || organizationid==null){
            return AjaxResult.error("数据异常！！！");
        }
        sysOrganizationService.updateSysOrganizationState(organizationid);
        return AjaxResult.success();
    }



    @PutMapping("/updateSysOrganization")
    @ApiOperation("修改机构信息")
    public AjaxResult updateSysOrganization(SysOrganization sysOrganization){
        sysOrganizationService.updateSysOrganization(sysOrganization);
        return AjaxResult.success();
    }

    /**
     * 删除机构信息
     */
    @DeleteMapping("/deleteSysOrganization/{organizationid}")
    public AjaxResult deleteSysOrganization(@PathVariable Integer organizationid){
        sysOrganizationService.deleteSysOrganization(organizationid);
        return AjaxResult.success();
    }


    /**
     * 添加机构信息
     */
    @PostMapping("/saveSysOrganization")
    @ApiOperation("添加机构信息")
    public AjaxResult saveSysOrganization(@RequestParam("sealfile") MultipartFile sealfile,
                                          @RequestParam("organizationname")String organizationname,
                                          @RequestParam("file")String file,
                                          @RequestParam("filemsg")String filemsg,
                                          @RequestParam("create_by")String create_by,
                                          @RequestParam("update_by")String update_by){
        SysOrganization sysOrganization=new SysOrganization();
        sysOrganization.setSealfile(sealfile);
        sysOrganization.setFile(file);
        sysOrganization.setOrganizationname(organizationname);
        sysOrganization.setFilemsg(filemsg);
        sysOrganization.setCreate_by(create_by);
        sysOrganization.setUpdate_by(update_by);
        sysOrganizationService.saveSysOrganization(sysOrganization);
        return AjaxResult.success();
    }


    /**
     * 查询所有机构信息用于填充前端下拉框数据
     */
    @GetMapping("/querySysOrganization")
    public AjaxResult querySysOrganization(SysOrganization sysOrganization){
        if(sysOrganization.getCurrentPage()==null || sysOrganization.getPageSize()==null){
            return AjaxResult.success(sysOrganizationService.querySysOrganization(sysOrganization));
        }
        PageHelper.startPage(sysOrganization.getCurrentPage(),sysOrganization.getPageSize());
        List<SysOrganization> sysOrganizations = sysOrganizationService.querySysOrganization(sysOrganization);
        PageInfo<SysOrganization>  pageInfo=new PageInfo<>(sysOrganizations);
        return AjaxResult.success(pageInfo);
    }

     /**
      * 查询机构名称和id
      *
      * @return 对象集合
      * */
     @GetMapping("/querOrganizationName")
     public AjaxResult querOrganizationName() {
          List<SysOrganization> sysOrganizations = sysOrganizationService.querOrganizationName();
          return AjaxResult.success(sysOrganizations);
     }

}
